const app = getApp();
var map;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabActived: 0, // 当前选中的Tab页 0 : 发现 1：附近
    tabScroll: '200px', // scroll高度
    scrollY: true,
    start: 1, // 贴子开始页数
    limit: 10, // 每页条数
    total: 0, // 总条数
    typeArray: [{ name: '全部', id: 1 }, { name: '古树名木', id: 1 }, { name: '花', id: 1 }],
    typeIndex: 0,
    radiusArray: [{ name: '3公里', value: 3000 }, { name: '5公里', value: 5000 }, { name: '10公里', value: 10000 }, { name: '30公里', value: 30000 }, { name: '50公里', value: 50000 } ],
    radiusIndex: 0,
    mapHeight: '300px',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      tabScroll: app.globalData.windowHeight - app.globalData.screenHeight / 1334 * (80) + 'px',
      mapHeight: app.globalData.windowHeight - app.globalData.screenHeight / 1334 * (178) + 'px',
    })
    this.getColumn(true)
    this.getLocation()
  },
  onReachBottom: function () {
    console.log('1111')
  },
  chooseTab: function (e) {
    let index = parseInt(e.currentTarget.dataset.index);
    if (index == this.data.tabActived) return
    this.setData({
      tabActived: index
    })
    if (index == 0) {
      this.setData({
        postList: [],
        // 页码重新计数
        start: 1,
      })
      this.getColumn(true)
    }
  },
  changeColumn: function (e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      column: id,
      postList: [],
      // 页码重新计数
      start: 1,
    })
    this.getPostList(true)
  },
  /**
 * 获取栏目信息
 * @params type
 */
  getColumn: function (isLoading) {
    app.get('posts/getColumn', { type: this.data.tabActived })
      .then(res => {
        this.setData({
          columnList: res,
          column: res[0].id
        })
        this.getPostList(isLoading)
      })
  },
  getPostList: function (isLoading = false) {
    app.get('posts/getList', { start: this.data.start, limit: this.data.limit, type: this.data.tabActived, column: this.data.column }, isLoading)
      .then(res => {
        let postList = res.list
        if (this.data.postList) {
          postList = [...postList, ...this.data.postList]
        }
        this.setData({
          postList: postList,
          total: res.total
        })
      })
  },
  getMore: function () {
    let start = this.data.start,
      limit = this.data.limit,
      total = this.data.total;
    if (start * limit >= total) {
      return
    } else {
      start += 1
      this.setData({
        start: start
      })
      this.getPostList(true)
    }
  },
  lookImg: function (e) {
    app.imgs(e.currentTarget.dataset.urls, e.currentTarget.dataset.src)
  },
  publish: function () {
    wx.showActionSheet({
      itemList: ['拍照', '录像'],
      success: (res) => {
        if (res.tapIndex == 0) {
          wx.navigateTo({
            url: '/pages/publish/publish?type=0'
          })
        } else {
          wx.navigateTo({
            url: '/pages/publish/publish?type=1'
          })
        }
      }
    })
  },
  /**
  * 点赞
  * @params id
  */
  like: function (e) {
    let index = e.currentTarget.dataset.index,
      id = this.data.postList[index].id,
      postList = this.data.postList;
    app.get('posts/like', { id: id })
      .then(() => {
        postList[index].likes += 1;
        postList[index].isLike = 1;
        this.setData({
          postList: postList
        })
      })
  },
  changeAddr: function (e) {
    this.setData({
      addrIndex: e.detail.value
    })
  },
  changeRadius: function (e) {
    this.setData({
      radiusIndex: e.detail.value
    })
    let circles = this.data.circles;
    circles[0].radius = this.data.radiusArray[e.detail.value].value
    this.setData({
      circles: circles
    })
  },
  /**
   * 地图
   */
  getLocation: function () {
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
       this.setData({
         location: {
           lat: res.latitude,
           lon: res.longitude
         },
         circles: [{
           latitude: res.latitude,
           longitude: res.longitude,
           color: '#FF0000DD',
           fillColor: '#7cb5ec88',
           radius: 3000,
           strokeWidth: 1
         }]
       })
       let markers = [];
       for (let i = 0; i < 5; i++) {
         let obj = {
           iconPath: "../../image/icon/camera.png",
           id: 0,
           latitude: res.latitude + Math.random() *(-0.01),
           longitude: res.longitude + Math.random() * (-0.01),
           width: 30,
           height: 30
         }
         markers.push(obj);
       }
       this.setData({
         markers: markers
       })
      }
    })
  },
  regionchange: function () {
  },
  controltap: function (e) {
    switch (e.controlId) {
      case 1: {
        map.moveToLocation();
        break;
      }
    }
  },
  markertap: function () {
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (!map) {
      map = wx.createMapContext('map');
    }
    var info = wx.getSystemInfoSync();
    this.setData({
      controls: [{
        id: 1,
        position: { left: info.windowWidth - 50, top: info.windowHeight - 200, width: 30, height: 30 },
        clickable: true,
        iconPath: '../../image/icon/map-1.png'
      }]
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})